package com.ww.dao.impl;

import com.ww.dao.CommentDao;
import com.ww.entity.Comment;
import com.ww.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;


/**
 * 讨论表的实现类
 */
public class CommentDaoImpl implements CommentDao {
    Connection conn = DBUtil.getConnection();
    QueryRunner qr = new QueryRunner();

    @Override
    public int save(Comment obj) {
        String sql = "INSERT INTO  tb_comment(comment_id,user_id,goods_id,comment_date,comment_content,comment_type) VALUES (?,?,?,?,?,?)";
        Object[] paras = {obj.getComment_id(),obj.getUser_id(),obj.getGoods_id(),obj.getComment_date(),obj.getComment_content()
        ,obj.getComment_type()};
        Comment comment = null;
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Comment obj) {
        String sql = "UPDATE  tb_comment SET  user_id=?,goods_id=?,comment_date=?,comment_content=?,comment_type=? WHERE comment_id=? ";
        Object[] paras = {obj.getUser_id(),obj.getGoods_id(),obj.getComment_date(),obj.getComment_content()
                ,obj.getComment_type(),obj.getComment_id()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Object id) {
        String sql = "DELETE FROM tb_comment WHERE comment_id  = ?  ";
        Object[] paras = {id};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Comment findById(Object id) {
        String sql = "SELECT * FROM tb_comment where comment_id  = ?";
        Object[] paras = {id};
        try {
            Comment comment = qr.query(conn, sql, paras, new BeanHandler<Comment>(Comment.class));
            return comment;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Comment> findAll() {
        String sql = "SELECT * FROM tb_comment";
        Object[] paras = {};
        try {
            List<Comment> commentList = qr.query(conn, sql, paras, new BeanListHandler<Comment>(Comment.class));
            return commentList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Comment> findSearch(String keyword) {
        String sql = "SELECT * FROM tb_comment where comment_content like concat('%',concat(?,'%'))";
        Object[] paras = {keyword};
        try {
            List<Comment> commentList = qr.query(conn, sql, paras, new BeanListHandler<Comment>(Comment.class));
            return commentList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Comment> findPage(int page, int pagesize) {
        String sql = "SELECT * FROM tb_comment LIMIT ?,?";
        Object[] paras = {(page-1)*pagesize,pagesize};
        try {
            List<Comment> commentList = qr.query(conn, sql, paras, new BeanListHandler<Comment>(Comment.class));
            return commentList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long count() {
        String sql = "select count(*) from tb_comment";
        Object[] paras = {};
        try {
            long nums = qr.query(conn, sql, paras, new ScalarHandler<Long>());
            return nums;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
